package com.xbongbong.paas.elasticsearch.util;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.enums.subform.SubFormTransferEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;

import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author 吴峰
 * @date 2021/12/24 14:32
 */
public class ElasticSearchConditionUtil {


    /**
     * 包装查询参数（包含负责人）
     *
     * @param dataId 数据id
     * @param userIdIn 员工idIn
     * @param corpid 公司id
     * @param indexTypeEnum
     * @return org.elasticsearch.index.query.BoolQueryBuilder
     * @author 徐俊杰
     * @date 2019/7/4 11:08
     * @since v1.0
     */
    public static BoolQueryBuilder permissionQueryIsMain(Long dataId, List<String> userIdIn, String corpid, IndexTypeEnum indexTypeEnum) {
        BoolQueryBuilder boolQueryBuilder = permissionQuery(dataId, userIdIn, corpid, indexTypeEnum);
        boolQueryBuilder.filter(termQuery(SubFormTransferEnum.getEsAttrByIndexTypeAndDbAttr(indexTypeEnum, ParameterConstant.IS_MAIN), BasicConstant.MAIN_USER));
        return boolQueryBuilder;
    }

    /**
     * 包装查询参数
     *
     * @param dataId 数据id
     * @param userIdIn 员工idIn
     * @param corpid 公司id
     * @param indexTypeEnum
     * @return org.elasticsearch.index.query.BoolQueryBuilder
     * @author 徐俊杰
     * @date 2019/7/4 11:08
     * @since v1.0
     */
    public static BoolQueryBuilder permissionQuery(Long dataId, List<String> userIdIn, String corpid, IndexTypeEnum indexTypeEnum) {
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery(SubFormTransferEnum.getEsAttrByIndexTypeAndDbAttr(indexTypeEnum, FieldTypeEnum.DATAID.getAlias()), dataId));
        boolQueryBuilder.filter(termQuery("del", DelEnum.NORMAL.getDel()));
        boolQueryBuilder.filter(termsQuery(SubFormTransferEnum.getEsAttrByIndexTypeAndDbAttr(indexTypeEnum, ParameterConstant.USER_ID), userIdIn));
        return boolQueryBuilder;
    }
}
